import json
import os
import sys

from .log import logger


class Settings(object):
    # 单例模式
    _instance = None
    _settings = {
        'General': {
            'Enable': True,
            'Mode': 'auto',
            'API': 'Baidu',
        },
        'Appearance': {
            'Position': 'right',
            'FontFamily': 'Times New Roman',
            'FontSize': 20,
            'FontColor': 'black',
            'Margin': 10,
            'Opacity': 200,
        },
        'Animation': {
            'Enable': True,
            'Duration': 500,
        },
        'Baidu': {
            'AppID': '',
            'SecretKey': '',
        },
        'Youdao': {
            'AppID': '',
            'SecretKey': '',
            'Domain': 'general',
            'RejectFallback': False,
        }
    }

    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super().__new__(cls)
        return cls._instance

    def __init__(self, *args, **kwargs):
        scr_path = os.path.abspath(os.path.dirname(sys.argv[0]))
        self.config_path = os.path.join(scr_path, 'config.json')

        if args:
            self.config_path = args[0]
        if kwargs:
            self.config_path = kwargs.get('config_path', self.config_path)

    def read_config(self, *args, **kwargs):
        path = self.config_path

        if args:
            path = args[0]
        if kwargs:
            path = kwargs.get('config_path', self.config_path)

        try:
            with open(path, 'r') as f:
                config_dict = json.load(f)
                for key, value in config_dict.items():
                    self._settings[key].update(value)
            logger.debug(f'Config file loaded: {path}')
        except FileNotFoundError:
            logger.debug(f'Config file not found: {path}')

    def write_config(self, *args, **kwargs):
        path = self.config_path

        if args:
            path = args[0]
        if kwargs:
            path = kwargs.get('config_path', self.config_path)

        with open(path, 'w') as f:
            json.dump(self._settings, f, ensure_ascii=False, indent=4)
        logger.debug(f'Config file saved: {path}')

    @property
    def enable(self) -> bool:
        return self._settings['General']['Enable']

    @enable.setter
    def enable(self, value: bool):
        self._settings['General']['Enable'] = value

    @property
    def trans_mode(self) -> str:
        return self._settings['General']['Mode']

    @trans_mode.setter
    def trans_mode(self, value: str):
        self._settings['General']['Mode'] = value

    @property
    def trans_api(self) -> str:
        return self._settings['General']['API']

    @trans_api.setter
    def trans_api(self, value: str):
        self._settings['General']['API'] = value

    @property
    def pos(self) -> str:
        return self._settings['Appearance']['Position']

    @pos.setter
    def pos(self, value: str):
        self._settings['Appearance']['Position'] = value

    @property
    def opacity(self) -> int:
        return self._settings['Appearance']['Opacity']

    @opacity.setter
    def opacity(self, value: int):
        self._settings['Appearance']['Opacity'] = value

    @property
    def margin(self) -> int:
        return self._settings['Appearance']['Margin']

    @margin.setter
    def margin(self, value: int):
        self._settings['Appearance']['Margin'] = value

    @property
    def font_family(self) -> str:
        return self._settings['Appearance']['FontFamily']

    @font_family.setter
    def font_family(self, value: str):
        self._settings['Appearance']['FontFamily'] = value

    @property
    def font_size(self) -> int:
        return self._settings['Appearance']['FontSize']

    @font_size.setter
    def font_size(self, value: int):
        self._settings['Appearance']['FontSize'] = value

    @property
    def font_color(self) -> str:
        return self._settings['Appearance']['FontColor']

    @font_color.setter
    def font_color(self, value: str):
        self._settings['Appearance']['FontColor'] = value

    @property
    def baidu_app_id(self) -> str:
        return self._settings['Baidu']['AppID']

    @baidu_app_id.setter
    def baidu_app_id(self, value: str):
        self._settings['Baidu']['AppID'] = value

    @property
    def baidu_secret_key(self) -> str:
        return self._settings['Baidu']['SecretKey']

    @baidu_secret_key.setter
    def baidu_secret_key(self, value: str):
        self._settings['Baidu']['SecretKey'] = value

    @property
    def youdao_app_id(self) -> str:
        return self._settings['Youdao']['AppID']

    @youdao_app_id.setter
    def youdao_app_id(self, value: str):
        self._settings['Youdao']['AppID'] = value

    @property
    def youdao_secret_key(self) -> str:
        return self._settings['Youdao']['SecretKey']

    @youdao_secret_key.setter
    def youdao_secret_key(self, value: str):
        self._settings['Youdao']['SecretKey'] = value

    @property
    def youdao_domain(self) -> str:
        return self._settings['Youdao']['Domain']

    @youdao_domain.setter
    def youdao_domain(self, value: str):
        self._settings['Youdao']['Domain'] = value

    @property
    def youdao_reject_fallback(self) -> bool:
        return self._settings['Youdao']['RejectFallback']

    @youdao_reject_fallback.setter
    def youdao_reject_fallback(self, value: bool):
        self._settings['Youdao']['RejectFallback'] = value

    @property
    def enable_anim(self) -> bool:
        return self._settings['Animation']['Enable']

    @enable_anim.setter
    def enable_anim(self, value: bool):
        self._settings['Animation']['Enable'] = value

    @property
    def duration(self) -> int:
        return self._settings['Animation']['Duration']

    @duration.setter
    def duration(self, value: int):
        self._settings['Animation']['Duration'] = value
